package com.sijia.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sijia.reggie.comment.R;
import com.sijia.reggie.entity.Category;
import com.sijia.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 杨思佳
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService cs;


    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> addCategory(@RequestBody Category category) {
        log.info("" + category);
        cs.save(category);
        return R.success("新增分类成功");
    }


    /**
     * 分页查询菜品分类
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> getCategoryPage(int page, int pageSize) {
        Page<Category> categoryPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        return R.success(cs.page(categoryPage, queryWrapper));
    }


    /**
     * 根据id删除菜品分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteCategory(@RequestParam(value = "ids") Long id) {
        cs.deleteCategory(id);
        return R.success("删除成功");
    }


    /**
     * 根据id修改菜品分类信息
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String> updateCategory(@RequestBody Category category) {
        cs.updateById(category);
        return R.success("信息更新成功");
    }

    /**
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> findByType(Integer type) {
        if (type == null){
            return R.success(cs.list());
        }
        return R.success(cs.findByType(type));
    }

}
